//
//  BKUserRetirePwdController.m
//  bestkeep
//
//  Created by windCould on 16/12/14.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKUserRetirePwdController.h"
#import "BKUserModifyPwdView.h"
#import "BKMyCommon.h"
#import "BKUserCodeFormat.h"
#import "BKUserUnitRequestAPI.h"
@interface BKUserRetirePwdController ()<UITextFieldDelegate>

//@property (nonatomic,strong)BKUserModifyPwdView * oldPwdView;
@property (nonatomic,strong)BKUserModifyPwdView * changedPwdTexView;
@property (nonatomic,strong)BKUserModifyPwdView * reWritePwdView;
@property (nonatomic,strong)UIButton * completeBtn;
@property (nonatomic,strong)UIView * bgView;


@end


@implementation BKUserRetirePwdController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"找回密码";
   // self.view.backgroundColor = ThemeBackgroundColor;
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self setUpViews];
}

#pragma mark =========================Function==============================

-(void)completeModifyPwd
{
    [self.view endEditing:YES];
    
    if (![BKUserCodeFormat parameterValueIsNotNilAndZeroLengthWithContentStr:_changedPwdTexView.contentTexfield.text]) {
        
        NSLog(@"请输入新密码");
        [HUD showErrorMessage:@"请输入新密码"];
        return;
    }
    
    if (![BKUserCodeFormat userPwdIsContainSpaceWithPwdStr:_changedPwdTexView.contentTexfield.text]) {
        
        [HUD showErrorMessage:@"密码不能包含空格"];
        return;
    }
    
    if (![BKUserCodeFormat userPwdCodeFormatIsRigthWithUserPwdStr:_changedPwdTexView.contentTexfield.text]) {
        
        NSLog(@"新的密码的格式不正确");
        [HUD showErrorMessage:@"请重新输入密码，英文或数字或 - _"];
        return;
    }
  
    NSInteger num = [BKUserCodeFormat stringLengthIsRightWIthRequireMinLength:6 maxLength:18 contentStr:_changedPwdTexView.contentTexfield.text];
    if (num == 6) {
        
        NSLog(@"密码必须位6-19位");
        [HUD showErrorMessage:@"密码不能少于6位"];
        return;
    }
    
    if (num == 18) {
        
        [HUD showErrorMessage:@"密码不能大于18位"];
        return;
    }

    if (![BKUserCodeFormat parameterValueIsNotNilAndZeroLengthWithContentStr:_reWritePwdView.contentTexfield.text]) {
        
        [HUD showErrorMessage:@"请再次输入新密码"];
        return;
    }

    if (![_changedPwdTexView.contentTexfield.text isEqualToString:_reWritePwdView.contentTexfield.text]) {
        
        NSLog(@"两次输入的密码不相等");
        [HUD showErrorMessage:@"确定密码与新密码不一致"];
        return;
    }
    
    [BKIndicationView showInView:self.view];
    [BKUserUnitRequestAPI findPwdAndResetNewPwdWithAccount:self.phoneNumber newPwd:_changedPwdTexView.contentTexfield.text passDataStr:self.passData resultBlock:^(id obj, NSError *error) {
        
        [BKIndicationView dismiss];
        if (obj) {
            
            BKUserRequestResultModel * result = obj;
            if ([result.success isEqualToString:@"1"]) {
                
                NSLog(@"%@",result.msg);
                [HUD showSuccessMessage:@"修改密码成功"];
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            } else {
            
                NSLog(@"%@",result.msg);
                [HUD showErrorMessage:result.msg];
            }
            
        } else {
        
            NSLog(@"网络请求失败。。。。。。。");
            if (error.code==502) {
                [HUD showErrorMessage:@"服务器开小差了，请稍后再试！"];
            }else{
                [HUD showErrorMessage:error.localizedDescription];
            }
        }
    
    }];
}

#pragma mark =========================布局===================================
-(void)setUpViews
{
    
    _bgView = [[UIView alloc]init];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_bgView];
    
//    _oldPwdView = [[BKUserModifyPwdView alloc]initWithFrame:CGRectZero];
//    _oldPwdView.contentTexfield.placeholder = @"请输入旧密码";
//    _oldPwdView.contentTexfield.secureTextEntry = YES;
//    _oldPwdView.contentTexfield.delegate = self;
//    _oldPwdView.leftImgUrlStr = @"BK_My_butter_icon_03";
//    _oldPwdView.rightImgUrlStrs = nil;
//    [self.view addSubview:_oldPwdView];
    
    _changedPwdTexView = [[BKUserModifyPwdView alloc]initWithFrame:CGRectZero];
    _changedPwdTexView.contentTexfield.placeholder = @"请输入6-18新密码";
    _changedPwdTexView.contentTexfield.secureTextEntry = YES;
    _changedPwdTexView.contentTexfield.delegate = self;
    _changedPwdTexView.leftImgUrlStr = @"icon_my_2";
    _changedPwdTexView.rightImgUrlStrs = @[@"BK_My_icon_eye",@"BK_My_icon_geye"];
    [self.view addSubview:_changedPwdTexView];
    
    _reWritePwdView = [[BKUserModifyPwdView alloc]initWithFrame:CGRectZero];
    _reWritePwdView.contentTexfield.placeholder = @"请再次输入新密码";
    _reWritePwdView.contentTexfield.secureTextEntry = YES;
    _reWritePwdView.contentTexfield.delegate = self;
    _reWritePwdView.leftImgUrlStr = @"icon_my_2";
    _reWritePwdView.rightImgUrlStrs = @[@"BK_My_icon_eye",@"BK_My_icon_geye"];
    [self.view addSubview:_reWritePwdView];
    
    _completeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _completeBtn.titleLabel.font = UIFontBoldCustom(18);
    [_completeBtn setTitle:@"确认" forState:UIControlStateNormal];
    _completeBtn.layer.cornerRadius = 4;
    _completeBtn.layer.masksToBounds = YES;
    [_completeBtn addTarget:self action:@selector(completeModifyPwd) forControlEvents:UIControlEventTouchUpInside];
    _completeBtn.backgroundColor = BK_MY_COLOR_01;
    [self.view addSubview:_completeBtn];
    
    [self layoutViews];
    
}

-(void)layoutViews
{
    
//    [_oldPwdView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.top.mas_equalTo(self.view.mas_top).offset(0);
//        make.left.mas_equalTo(self.view.mas_left).offset(10);
//        make.right.mas_equalTo(self.view.mas_right).offset(-10);
//        make.height.mas_equalTo(50);
//    }];
    
    
    [_changedPwdTexView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.view.mas_top).offset(0);
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.height.mas_equalTo(50);
    }];
    
    [_reWritePwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_changedPwdTexView.mas_bottom).offset(0);
        make.left.mas_equalTo(_changedPwdTexView.mas_left).offset(0);
        make.right.mas_equalTo(_changedPwdTexView.mas_right).offset(0);
        make.height.mas_equalTo(@(50));
    }];
    
    [_completeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_reWritePwdView.mas_bottom).offset(20);
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.height.mas_equalTo(@(45));
    }];
    
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.view.mas_top).offset(0);
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.right.mas_equalTo(self.view.mas_right).offset(0);
        make.bottom.mas_equalTo(_reWritePwdView.mas_bottom).offset(0);
    }];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return  YES;
}



@end
